package com.lc.demo.controller;

import com.lc.demo.bean.User;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.UserService;
import com.lc.demo.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: sakila
 * @Date: 2022/03/06/12:19
 * @Description:
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping()
    public ResponseMessage login(HttpServletRequest request) {
        //1. 获取请求
        // 4. 获取请求头
        String authorization = request.getHeader("Authorization");
        // 5. 请求头中获取令牌
        Claims claims = JwtUtil.parseJWT(authorization);
        String subject = claims.getSubject();
        User userFind=new User();
        userFind.setEmail(subject);
        User user= userService.findOne(userFind);
        user.setPassword("");
        return ResponseMessage.successWithData(user);
    }


    @PutMapping()
    public ResponseMessage userInfoUpdate(@RequestBody User user,HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        // 5. 请求头中获取令牌
        Claims claims = JwtUtil.parseJWT(authorization);
        String subject = claims.getSubject();
        User userFind=new User();
        userFind.setEmail(subject);
        User one = userService.findOne(userFind);
        user.setId(one.getId());
        userService.userInfoUpdate(user);
        return ResponseMessage.success();
    }


}
